package com.erpanderp.wam.adm.model.service;

import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.hibernate.Session;

import com.erpanderp.wam.adm.model.entity.RoleService;
import com.erpanderp.wam.adm.model.entity.RoleServiceGroup;
import com.erpanderp.wam.adm.model.entity.Service;
import com.erpanderp.wam.adm.model.entity.ServiceGroup;
import com.erpanderp.wam.util.HibernateUtil;

public class RoleServiceDAO {
	
	/**
	 * Get Services based on employee ID and moduleID
	 * @param employeeId
	 * @param moduleId
	 * @return
	 */
	public List<Service> getRoleServices(Long employeeId, Long moduleId){
		List<Service> services = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			String queryString = "select rsg.service from RoleService rsg where rsg.role.roleId in (select er.role.roleId from EmployeeRole er where er.employee.employeeId = :employeeId) and rsg.service.module.moduleId = :moduleId";
			Query query = session.createQuery(queryString).setParameter("moduleId", moduleId).setParameter("employeeId", employeeId);
			query.setCacheable(false);
			
			services = (List<Service>) query.list();	
			
			session.close();
			return services;
			
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		
	}
	
	public List<RoleService> getRoleServicesByRoleId(Long roleId) {
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from RoleService rs where rs.role.roleId=:roleId";
			Query query = session.createQuery(queryString).setLong("roleId",
					roleId);
			query.setCacheable(false);
			return (List<RoleService>) query.list();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}		
	}

	public boolean deleteRoleService(RoleService roleService) {
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			session.delete(roleService);
			session.getTransaction().commit();
			return true;
			
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public RoleService getRoleService(Long roleId, Long serviceId) {
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from RoleService rs where rs.role.roleId=:roleId and rs.service.serviceId = :serviceId";
			Query query = session.createQuery(queryString).setLong("roleId",
					roleId).setLong("serviceId", serviceId);
			query.setCacheable(false);
			List temp = (List<RoleService>) query.list();
			if(temp != null && temp.size() > 0)
				return (RoleService) temp.get(0);
			else
				return null;
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public boolean deleteRoleServices(List<RoleService> roleServices) {
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			for(RoleService roleService : roleServices){
				session.delete(roleService);	
			}
			session.getTransaction().commit();
			return true;
			
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
}
